﻿using LS.FrmUI.Dal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace LS.FrmUI
{
    public partial class FrmLogin : Form
    {
        UserInfoDal dal = new UserInfoDal();
        public FrmLogin()
        {
            InitializeComponent();

            /*skin*/

            //skinEngine1.SkinFile = Application.StartupPath + @"\Warm.ssk";

            //Sunisoft.IrisSkin.SkinEngine se = null;
            //se = new Sunisoft.IrisSkin.SkinEngine();
            //se.SkinAllForm = true;
            /*skin*/

            DataTable dt = dal.GetAllUser();
            if (dt.Rows.Count>0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    cboUser.Items.Add(Convert.ToString(dr["UserName"]));
                }
            }
            cboUser.SelectedIndex = 2;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = cboUser.Text.Trim();
            string password = txtPwd.Text;
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入用户名和密码");
                txtPwd.Focus();
                return;
            }
            bool logRes =dal.UserLogin(userName, password);
            if (logRes)
            {
                setting.UserRole = lblRole.Text;
                setting.UserName = cboUser.Text;
                new FrmMain().Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("用户名密码错误。");
                txtPwd.Text = "";
                txtPwd.Focus();
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.Text += "   程序版本号:"+Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private void cboUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblRole.Text = dal.GetRoleByUserName(cboUser.Text);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
